package lagou.cn.homework
import scala.util.Random
import scala.beans.BeanProperty

/**
  *         1. 选取对战角色
  *
  *         2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
  *
  *         3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
  *
  *         4 . 循环对战，当输入“n”时，终止对战，并显示对战结果
  *
  *         5. 游戏结束后显示得分
  */
class User(){

  @BeanProperty var name:String = "游客"
  @BeanProperty var score:Int =_
  @BeanProperty var win:Int =_
  @BeanProperty var tie:Int =_
  @BeanProperty var lose:Int =_

  def showFist():Int = {
    println("请出拳！1.剪刀 2.石头 3.布")
    val select = scala.io.StdIn.readInt()
    if(select != 1 && select != 2 && select != 3) {
      println("输入不符合规范，默认出布!")
      return 3
    } else return select
  }
}

class Computer(){

  @BeanProperty var name:String = _
  @BeanProperty var score:Int =_
  @BeanProperty var win:Int =_
  @BeanProperty var tie:Int =_
  @BeanProperty var lose:Int =_

  def showFist():Int = {
    new Random().nextInt(3)+1
  }
}

class Game(){

  @BeanProperty var user: User = _
  @BeanProperty var computer: Computer =_
  @BeanProperty var count:Int =_

  def play(): Unit = {
    var flag:Boolean = true
    while (flag){
      val userFist = user.showFist()
      val computerFist = computer.showFist()
      println(s"你出拳：${if(userFist == 1) "剪刀" else if(userFist == 2) "石头" else "布"}")
      println(s"${computer.getName}出拳！")
      println(s"${computer.getName}出拳:${if(computerFist == 1) "剪刀" else if(computerFist == 2) "石头" else "布"}")

      var result = if (userFist == computerFist) 1 else {
        if ((userFist - computerFist) == -1 || (userFist - computerFist) == 2) 0 else 2
      }

      if(result == 1){
        user.score += 1
        computer.score += 1
        user.tie += 1
        computer.tie += 1
        println("和局！下次继续努力")
      } else if(result == 2){
        user.score +=2
        user.win += 1
        computer.lose +=1
        println("恭喜，你赢啦")
      } else {
        computer.score += 2
        computer.win += 1
        user.lose += 1
        println("你输了")
      }
      count += 1

      println("是否开始下一轮？y/n")
      val option = scala.io.StdIn.readChar()
      if(option == 'n') {
        flag = false
        println("退出游戏!")
      }
    }
  }

}


object TaskTwoDemo {
  def main(args: Array[String]): Unit = {

    println("-----------------欢迎进入游戏世界----------------------")
    println("******************************")
    println("************猜拳，开始********")
    println("******************************")
    println("请选择对战角色:(1.刘备 2.关羽 3.张飞)")
    val role = scala.io.StdIn.readInt()
    val roleName = role match {
      case 1 => "刘备"
      case 2 => "关羽"
      case 3 => "张飞"
      case _ => "选择错误"
    }
    println(s"你选择了与${roleName}对战")
    println("要开始么？y/n")
    val select = scala.io.StdIn.readChar()

    if(select == 'y'){
      val user = new User()
      val computer = new Computer()
      computer.name = roleName

      val game = new Game()
      game.setUser(user)
      game.setComputer(computer)
      game.play()
      println("-------------------------------------------------------------")
      println(s"${game.computer.name} VS ${game.user.name}")
      println(s"对战次数${game.count}次")
      println(s"姓名\t得分\t胜局\t和局\t负局")
      println(s"${game.user.name}\t${game.user.score}\t${game.user.win}\t${game.user.tie}\t${game.user.lose}")
      println(s"${game.computer.name}\t${game.computer.score}\t${game.computer.win}\t${game.computer.tie}\t${game.computer.lose}")
      println("-------------------------------------------------------------")
    } else {
      println("退出游戏！")
    }
  }
}
